package cn.imvc.app.controllers.seller;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.servlet.ViewRendererServlet;

import cn.imvc.app.controllers.goods.GoodsControllerImpl;
import cn.imvc.app.entities.Area;
import cn.imvc.app.entities.Express;
import cn.imvc.app.entities.ExpressFee;
import cn.imvc.app.models.AreaModel;
import cn.imvc.app.models.ExpressFeeModel;
import cn.imvc.app.models.ExpressModel;
import cn.imvc.app.models.LoggingModel;
import cn.imvc.core.DialogBean;
import cn.imvc.core.Render;
import cn.imvc.data.domain.ViewRequest;

public class ExpressFeeControllerImpl implements ExpressFeeController {
	static Logger logger = Logger.getLogger(ExpressFeeControllerImpl.class);
	public Page<ExpressFee> page;
	public ViewRequest viewRequest;
	public ExpressFee expressFee;
	public int id;
	public int[] ids;
	public Express express;
	public int expressId;
	public String search_content;
	public int areaId;
	public Area area;
    private double theFirstPrice;
	private double theAdditionalPrice;
	    
	@Autowired
	ExpressFeeModel expressFeeModel;
	@Autowired
	ExpressModel expressModel;
	@Autowired
	AreaModel areaModel;
	@Autowired
	LoggingModel loggingModel;
	public String search() {
		return null;
	}

	public String index() {
		Map<String, String> searchMap = new HashMap<String, String>();
		searchMap.put("expressId_eq_string", String.valueOf(expressId));
		if (viewRequest == null) {
			viewRequest = new ViewRequest();
		}
		viewRequest.setParams(searchMap);
		page = expressFeeModel.find(viewRequest);
		return "express-fee/index";
	}

	public String show() {
		
		return null;
	}

	public String editNew() {
		express = expressModel.get(expressId);
		return "express-fee/editNew";
	}

	public String create() {
		expressFee.setFirstPrice((int)(theFirstPrice*100));
		expressFee.setAdditionalPrice((int)(theAdditionalPrice*100));
		expressFee.setExpress(expressModel.get(expressId));
		expressFeeModel.save(expressId,areaId,expressFee);
		loggingModel.info(logger,"添加快递费用："+expressFee.getExpress());
		return Render.json(new DialogBean("200","添加成功","express_expressFee","closeCurrent"));
	}

	public String edit() {
		expressFee = expressFeeModel.get(id);
		express = expressFee.getExpress();
		area = expressFee.getArea();
		return "express-fee/edit";
	}

	public String update() {
		expressFee.setFirstPrice((int)(theFirstPrice*100));
		expressFee.setAdditionalPrice((int)(theAdditionalPrice*100));
		expressFee.setExpress(expressModel.get(expressId));
		expressFeeModel.update(id,expressId,areaId,expressFee);
		loggingModel.info(logger,"修改快递费用："+expressFee.getExpress());
		return Render.json(new DialogBean("200","修改成功","express_expressFee","closeCurrent"));
	}

	public String destroy() {
		expressFeeModel.delete(id);
		loggingModel.info(logger,"删除快递费用："+id);
		return Render.json(new DialogBean("200","删除成功","express_expressFee"));
	}

	public String destroyBatch() {
		expressFeeModel.deleteBatch(ids);
		return Render.json(new DialogBean("200","删除成功","express_expressFee"));
	}

	public ViewRequest getViewRequest() {
		return viewRequest;
	}

	public void setViewRequest(ViewRequest viewRequest) {
		this.viewRequest = viewRequest;
	}

	public ExpressFee getExpressFee() {
		return expressFee;
	}

	public void setExpressFee(ExpressFee expressFee) {
		this.expressFee = expressFee;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int[] getIds() {
		return ids;
	}

	public void setIds(int[] ids) {
		this.ids = ids;
	}

	public Express getExpress() {
		return express;
	}

	public void setExpress(Express express) {
		this.express = express;
	}

	public int getExpressId() {
		return expressId;
	}

	public void setExpressId(int expressId) {
		this.expressId = expressId;
	}

	public String getSearch_content() {
		return search_content;
	}

	public void setSearch_content(String search_content) {
		this.search_content = search_content;
	}

	public int getAreaId() {
		return areaId;
	}

	public void setAreaId(int areaId) {
		this.areaId = areaId;
	}

	public Area getArea() {
		return area;
	}

	public void setArea(Area area) {
		this.area = area;
	}

	public double getTheFirstPrice() {
		return theFirstPrice;
	}

	public void setTheFirstPrice(double theFirstPrice) {
		this.theFirstPrice = theFirstPrice;
	}

	public double getTheAdditionalPrice() {
		return theAdditionalPrice;
	}

	public void setTheAdditionalPrice(double theAdditionalPrice) {
		this.theAdditionalPrice = theAdditionalPrice;
	}

}
